package com.ssm.service;

import com.ssm.bean.Employee;
import com.ssm.bean.EmployeeExample;
import com.ssm.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;


    public void saveEmp(Employee employee){
        employeeMapper.insertSelective(employee);
    }
    public List<Employee>  getAll(){
        return employeeMapper.selectByExampleWithDept(null);
    }
    public boolean checkUser(String empName){
        EmployeeExample employeeExample=new EmployeeExample();
        EmployeeExample.Criteria  criteria=employeeExample.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count=employeeMapper.countByExample(employeeExample);
        return count==0;
    }

    /**
     * 修改员工信息之前，返回员工信息，通过ID查询员工
     * @param id
     * @return
     */
    public Employee getEmp(Integer id) {
        Employee employee=employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    /**
     * 修改员工后，保存已经修改信息的员工
     * @param employee
     */
    public void updateEmp(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    public void deleteEmp(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    public void deleteBatch(List<Integer> str_ids) {
        EmployeeExample example=new EmployeeExample();
        EmployeeExample.Criteria criteria=example.createCriteria();
        //delete from XXX where emp_id in (1,2,3)
        criteria.andEmpIdIn(str_ids);
        employeeMapper.deleteByExample(example);
    }
}
